package com.atguigu.gmall.web.all.controller;

import com.atguigu.gmall.feign.seckill.SeckillFeignClient;
import com.atguigu.gmall.feign.user.UserFeignClient;
import com.atguigu.gmall.model.activity.SeckillGoods;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.model.user.UserAddress;
import io.reactivex.rxjava3.internal.operators.maybe.MaybeDelayOtherPublisher;
import jodd.introspector.MethodParamDescriptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class ActivityController {

    @Autowired
    SeckillFeignClient seckillFeignClient;

    @Autowired
    UserFeignClient userFeignClient;

    /**
     * 秒杀商品列表
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String toSeckillPage(Model model){

        //查出所有参与当前时间秒杀的商品

        //skuId\skuDefaultImg\skuName\costPrice\price\num\stockCount
        List<SeckillGoods> currentSeckillGoods = seckillFeignClient.getCurrentSeckillGoods();
        model.addAttribute("list",currentSeckillGoods);

        return "seckill/index";
    }

    /**
     * 秒杀商品详情页
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String toSeckillGoodDetailPage(@PathVariable("skuId") Long skuId, Model model){

        //返回详情页 skuDefaultImg\skuName\SeckillGoods
        SeckillGoods detail = seckillFeignClient.getSeckillGoodsDetail(skuId);
        model.addAttribute("item",detail);


        //返回详情页
        return "seckill/item";
    }

    /**
     * queue.html?skuId=38&skuIdStr=5db6d2147a00389825872405a2513e66
     * 排队页面
     * 页面会每隔3秒给服务器发送请求检查是否成功
     *
     * @param skuId
     * @param skuIdStr
     * @param model
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String toSeckillQueuePage(@RequestParam("skuId") Long skuId,
                                     @RequestParam("skuIdStr") String skuIdStr,
                                     Model model){
        model.addAttribute("skuId",skuId);
        model.addAttribute("skuIdStr",skuIdStr);

        return "seckill/queue";
    }

    /**
     * 秒杀订单页面
     * @param model
     * @return
     */
    //seckill/trade.html
    @GetMapping("/seckill/trade.html")
    public String toTradePage(Model model){

        OrderInfo orderInfo = seckillFeignClient.getUserSeckillOrderInfo();
        model.addAttribute("detailArrayList",orderInfo.getOrderDetailList());
        model.addAttribute("totalNum",1);
        model.addAttribute("totalAmount",orderInfo.getTotalAmount());
        List<UserAddress> userAddress = userFeignClient.getCurrentUserAddress();
        model.addAttribute("userAddressList",userAddress);

        return "seckill/trade";
    }
}
